package com.xinkong.huangxunleiutils.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

import com.xinkong.huangxunleiutils.R;
import com.xinkong.huangxunleiutils.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 黄训磊 on 2017/1/4 16:57
 */

public class Animatios1Activity extends BaseActivity {

    @BindView(R.id.btn_rotate)
    Button mBtnRotate;
    @BindView(R.id.btn_scale)
    Button mBtnScale;
    @BindView(R.id.btn_alpha)
    Button mBtnAlpha;
    @BindView(R.id.btn_translate)
    Button mBtnTranslate;
    @BindView(R.id.img_first)
    ImageView mImgFirst;
    @BindView(R.id.btn_start_okhttp)
    Button mBtnStartOkhttp;

    @Override
    public int getLayoutId() {
        return R.layout.activity_animatios1;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        setTitle("动画");

    }

    @OnClick({R.id.btn_rotate, R.id.btn_scale, R.id.btn_alpha, R.id.btn_translate})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_rotate:
                rotateAnimation();
                break;
            case R.id.btn_scale:
                scaleAnimation();
                break;
            case R.id.btn_alpha:
                alphaAnimation();
                break;
            case R.id.btn_translate:
                translateAnimation();
                break;
        }
    }

    /**
     * 渐入渐出动画
     */
    private void alphaAnimation() {

        //创建一个AnimationSet对象，参数为boolean类型
        //参数true表示使用animatios的interpolater  ,false 则是使用自己
        AnimationSet animationSet = new AnimationSet(true);
        //创建一个AlphaAnimation对象   参数从完全透明到完全不透明
        AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);

        //设置动画执行的时间
        alphaAnimation.setDuration(500);

        //将alphAnimation动画添加到animationSet中
        animationSet.addAnimation(alphaAnimation);

        //使用ImageView的startAnimation方法执行动画
        mImgFirst.startAnimation(animationSet);

    }

    /**
     * 旋转动画
     */
    private void rotateAnimation() {
        AnimationSet animationSet = new AnimationSet(true);

        //  参数
        //float fromDegrees,  开始的角度
        // float toDegrees,   结束的角度
        // int pivotXType,     X轴的坐标类型
        //                    坐标类型有三种  ；Animation.ABSOLUTE, 绝对坐标
        //                                    Animation.RELATIVE_TO_SELF,相对于自身坐标
        //                                    Animation.RELATIVE_TO_PARENT. 相对于父控件的坐标
        // float pivotXValue,    x轴的坐标值   0.5f 表示 自身这个控件长度的一半
        //int pivotYType,      Y轴的坐标类型
        //float pivotYValue     Y轴的坐标值


        RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
                Animation.RELATIVE_TO_PARENT, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);


        rotateAnimation.setDuration(1000);
        animationSet.addAnimation(rotateAnimation);
        mImgFirst.startAnimation(animationSet);
    }

    /**
     * 缩放动画
     */
    private void scaleAnimation() {

        AnimationSet animationSet = new AnimationSet(true);
        //float fromX, float toX,
        // float fromY, float toY,
        //int pivotXType, float pivotXValue,
        // int pivotYType, float pivotYValue

        ScaleAnimation scaleAnimation = new ScaleAnimation(0, 0.5f, 0, 0.5f,
                Animation.RELATIVE_TO_PARENT, 0.5f,
                Animation.RELATIVE_TO_PARENT, 0.5f);
        scaleAnimation.setDuration(500);
        animationSet.addAnimation(scaleAnimation);
        mImgFirst.startAnimation(animationSet);

    }

    /**
     * 移动动画
     */
    private void translateAnimation() {
        AnimationSet animationSet = new AnimationSet(true);

        //int fromXType, float fromXValue,
        // int toXType, float toXValue,
        //int fromYType, float fromYValue,
        // int toYType, float toYValue

        TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0.5f);

        translateAnimation.setDuration(1000);
        animationSet.addAnimation(translateAnimation);
        mImgFirst.startAnimation(translateAnimation);
    }

    @OnClick(R.id.btn_start_okhttp)
    public void onClick() {
        startActivity(new Intent(this, OkHttpNetActivity.class));
    }
}
